/*!
 * jQuery Pagination Plugin
 *
 * Copyright (c) 2013
 * Dual licensed under the MIT and GPL licenses.
 *
 * @author bjlvyanmeng@jd.com
 * @version   0.1-2013.10.11
 * @dependency jQuery v1.9+
 *
 */


(function ($) {
    $.fn.jPagination = function (settings) {


        var $me = $(this);
        var UI_KEY = 'jui_jPagination';

        var $test = $me.data(UI_KEY);
        if ($test) {
            return $test;
        }


        var opts = $.extend({},$.fn.jPagination.defaults, settings);


        /**
         * 初始化分页条；
         */
        _initPagination();

        /**
         * 绑定事件；
         */
        _bindEvent();
        //==================================================================================================

        /**
         * 设置加载数据的参数
         * @param ajaxParam
         */
        this.setParam = function (param) {
            $.extend(opts, param);
        };

        $me.reload = function (param) {
            $.extend(opts, param);
            _initPagination();
            _bindEvent();
        };

        /**
         * 初始化分页条 ================================================================================================
         * @private
         */
        function _initPagination() {
            $me.empty();
            if(opts.totalSize > -1) {
                opts.pageSize = Math.ceil(opts.totalSize / opts.limit);
            }
            var html = "";
            if (opts.totalSize > 0) {

                var html_begin = '<div class="pz_fy" >';
                var html_pre = '<a title="上一页" class="pre_button a_1" style="cursor: pointer;" pageNo="pre"></a>';
                var html_next = '<a title="下一页" class="next_button a_5"  style="cursor: pointer;" pageNo="next"></a>';
                var html_total = '';
                var html_center = '';
//                html_total = '<span>共' + opts.pageSize + '页</span>';
                html_center = _getHtmlCenter();
                var html_end = '</div>';

                html = html_begin + html_pre + html_center + html_next + html_total  + html_end;
            }
//            var html_go = '<span>到</span><input type="text" class="text" value="' + opts.pageNum + '"/><span>页</span><p class="jd-page-sure">确定</p>';




            $me.append(html);
        }

        function _getHtmlCenter() {
            var html = '';
            if (opts.pageSize <= 5) {
                for (var j = 1; j <= opts.pageSize; j++) {
                    var item = '<a class="a_3"  style="cursor: pointer;" pageNO="' + j + '" >' + j + '</a>';
                    html = html + item;
                }
                return html;
            } else {
                var pageNum = opts.pageNum;
                var pageSize = opts.pageSize;
                if (pageNum <= 3) {
                    for (var i = 1; i <= 5; i++) {
                        var item = '<a class="a_3"  style="cursor: pointer;" pageNO="' + i + '">' + i + '</a>';
                        html = html + item;
                    }
                    //html = html + "<span class=''>...</span>";
                    return html;
                } else if (pageNum > 3 && pageNum < pageSize - 3) {
                    html = html + "<span class=''>...</span>";
                    for (var i = pageNum - 2; i <= pageNum + 2; i++) {
                        var item = '<a class="a_3"  style="cursor: pointer;" pageNO="' + i + '">' + i + '</a>';
                        html = html + item;
                    }
                    //html = html + "<span class=''>...</span>";
                    return html;
                } else {
                    //html = html + "<span class=''>...</span>";
                    for (var i = pageNum - 2; i <= pageSize; i++) {
                        var item = '<a class="a_3"  style="cursor: pointer;" pageNO="' + i + '">' + i + '</a>';
                        html = html + item;
                    }
                    return html;
                }
            }
        }

        /**
         * 绑定事件 ====================================================================================================
         * @private
         */
        function _bindEvent() {
            var $preButton = $me.find('.pre_button');
            var $nextButton = $me.find('.next_button');
            if(opts.totalSize > 0) {
                if(opts.pageNum == 1) {
                    $preButton.hide();
                } else {
                    $preButton.show();
                }
                if(opts.pageNum == opts.pageSize) {
                    $nextButton.hide();
                } else {
                    $nextButton.show();
                }
                if(opts.pageSize == 1) {
                    $nextButton.hide();
                    $nextButton.hide();
                    $('.pz_fy').find('a[pageNo="1"]').hide();
                }
            } else {
                if(opts.pageNum == 1) {
                    $preButton.removeClass("disabled").addClass("disabled");
                } else {
                    $preButton.removeClass("disabled");
                }
            }

            $me.find('.pz_fy').delegate('a', 'click', function () {

                var pageNo = $(this).attr('pageNo');
                var pageNum;
                var totalSize = opts.totalSize;
                var limit = opts.limit;
                if (pageNo == "pre") {
                    pageNum = parseInt(opts.pageNum) - 1;
                } else if (pageNo == "next") {
                    pageNum = parseInt(opts.pageNum) + 1;
                } else {
                    pageNum = parseInt(pageNo);
                }
                opts.pageNum = parseInt(pageNum);

                opts.onPaged.call($me, opts.totalSize, opts.limit, opts.pageNum);

                $me.empty();


                _initPagination();
                _bindEvent();


            });
            $me.find('.pz_fy').delegate('p[class="jd-page-sure"]', 'click', function () {
                var goPage = parseInt($me.find('.pz_fy').find('input').val());
                if(opts.pageSize && opts.pageSize > 0 && goPage > opts.pageSize) {
                    return false;
                }
                opts.pageNum = goPage;

                opts.onPaged.call($me, opts.totalSize, opts.limit, opts.pageNum);

                $me.empty();
                _initPagination();
                _bindEvent();
            });

            $me.find('.pz_fy').find('a[pageNo="' + opts.pageNum + '"]').removeClass("on").addClass("on");//给当前页加样式；
            $me.find('.pz_fy').find('input').val(opts.pageNum);//设置当前页输入框值；
        }

        $me.data(UI_KEY, $me);
        return $me;
    };

    $.fn.jPagination.defaults = {
        totalSize: -1,
        limit: 10,
        pageNum: 1,
        pageSize: 1,
        onPaged: function (pageNo, pageSize, totalSize) {

        }
    };

})(jQuery);